﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace TrackSystem.Entities.Extensions
{
    /// <summary>
    /// 通信协议
    /// </summary>
    public enum ProtocolEnum
    {
        /// <summary>
        /// 1.MODBUS
        /// </summary>
        Modbus=0,
        /// <summary>
        /// 2.电源板
        /// </summary>
        Power,
        /// <summary>
        /// 3.高度计
        /// </summary>
        Altimeter,
        /// <summary>
        /// 4.深度计
        /// </summary>
        Depth,
        /// <summary>
        /// 5.摄像头
        /// </summary>
        Camera,
        /// <summary>
        /// 6.操作杆
        /// </summary>
        Joystick,
        /// <summary>
        /// 7.MODBUS TCP
        /// </summary>
        ModbusTCP,
        /// <summary>
        /// 8.云台
        /// </summary>
        PanTilt,
        /// <summary>
        /// 9.罗盘
        /// </summary>
        Compass,
        /// <summary>
        /// 10.姿态(SCM220)
        /// </summary>
        TCM3,
        /// <summary>
        /// 11.CTD
        /// </summary>
        CTD,
        /// <summary>
        ///12. 推进器
        /// </summary>
        Thruster,
        /// <summary>
        /// 13.高度计
        /// </summary>
        ISA500,
        /// <summary>
        /// 14.姿态
        /// </summary>
        LEC210,
        /// <summary>
        /// 15.二号船罗经
        /// </summary>
        Ship2_gyro,
        /// <summary>
        /// 16.二号船GPS
        /// </summary>
        Ship2_GPS,
        /// <summary>
        /// 17.二号船USBL
        /// </summary>
        Ship2_USBL,
        /// <summary>
        /// 18.网络摄像头TCP控制协议
        /// </summary>
        NetCameraTCP,
        /// <summary>
        /// 19.绝缘检测协议
        /// </summary>
        Insulation,
        /// <summary>
        /// 20.惯导
        /// </summary>
        INS,
        /// <summary>
        /// 21.SID视频字符叠加
        /// </summary>
        SDI_OSD,
        /// <summary>
        /// 22.高度计
        /// </summary>
        HDCS200,
        /// <summary>
        /// 23.PTZ相机
        /// </summary>
        PTZCam,
        /// <summary>
        /// 24.Compass 3D
        /// </summary>
        Compass3D,
        /// <summary>
        /// 25.GPS_PGPVTG数据(深拖)
        /// </summary>
        Ship2_GPVTG,
        /// <summary>
        /// 26.GPSG数据(深拖)
        /// </summary>
        DTGPS,
        /// <summary>
        /// 27.Modbus协议,只读
        /// </summary>
        ModbusReadOnly,
        /// <summary>
        /// 28.声学所6000k DVL,只读
        /// </summary>
        DVL,
        /// <summary>
        /// 29. 绞车A架状态信息只读
        /// </summary>
        Winch,
        /// <summary>
        /// 30. 招商船舶罗经信息只读
        /// </summary>
        CMI_Gyro,
        /// <summary>
        /// 31. 招商HiPAP 超短基线信标读取
        /// </summary>
        CMI_USBL,
        /// <summary>
        /// 32. 招商HiPAP 长基线信标读取
        /// </summary>
        CMI_LBL,
        /// <summary>
        /// 33. 4K相机控制
        /// </summary>
        CAM4K,
        /// <summary>
        /// 34. PDU的PLC控制
        /// </summary>
        PLC_S71200,
        /// <summary>
        /// Seapath 42 惯导
        /// </summary>
        Seapath_42
    }

    /// <summary>
    /// 通道类型
    /// </summary>
    public enum PortCategory
    {
        /// <summary>
        /// TCP通信方式
        /// </summary>
        TCP = 0,   
        /// <summary>
        /// UDP通信方式
        /// </summary>
        UDP,        
        /// <summary>
        /// SERIAL通信方式
        /// </summary>
        SRIAL      
    }

    /// <summary>
    /// 通信协议参数
    /// </summary>
    public class ProtocolParams
    {
        /// <summary>
        /// 只读起始地址
        /// </summary>
        public int StartAdr_RO { get; set; } = -1;
        /// <summary>
        /// 只读数据长度
        /// </summary>
        public int RegLen_RO { get; set; } = -1;
        /// <summary>
        /// 读写起始地址
        /// </summary>
        public int StartAdr_RW { get; set; } = -1;
        /// <summary>
        /// 读写数据长度
        /// </summary>
        public int RegLen_RW { get; set; } = -1;

        /// <summary>
        /// 独占通道模式，默认为ture
        /// </summary>
        public bool ExclusiveMode { get; set; } = true;
    }

    /// <summary>
    /// 协议数据定义
    /// </summary>
    public static class ProtocolDefine
    {
        /// <summary>
        /// 已有协议参数
        /// </summary>
        public static ConcurrentDictionary<ProtocolEnum, ProtocolParams> Params = new ()
        {
            [ProtocolEnum.Modbus]       = new ProtocolParams{ StartAdr_RO = -1, RegLen_RO = -1, StartAdr_RW = -1, RegLen_RW = -1, ExclusiveMode=false },
            [ProtocolEnum.Power]        = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 10, StartAdr_RW = 0, RegLen_RW = 2, ExclusiveMode = false },
            [ProtocolEnum.Altimeter]    = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 1,  StartAdr_RW = 0, RegLen_RW = 0 },
            [ProtocolEnum.Depth]        = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 6,  StartAdr_RW = 0, RegLen_RW = 0 },
            [ProtocolEnum.Camera]       = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 0,  StartAdr_RW = 0, RegLen_RW = 11 },

            [ProtocolEnum.Joystick]     = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 27,  StartAdr_RW = 0, RegLen_RW = 0 },
            [ProtocolEnum.ModbusTCP]    = new ProtocolParams { StartAdr_RO = -1, RegLen_RO = -1, StartAdr_RW = -1, RegLen_RW = -1, ExclusiveMode = false },
            [ProtocolEnum.PanTilt]      = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 10,  StartAdr_RW = 0, RegLen_RW = 10 },
            [ProtocolEnum.Compass]      = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 3,   StartAdr_RW = 0, RegLen_RW = 2 },
            [ProtocolEnum.TCM3]         = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 10,  StartAdr_RW = 0, RegLen_RW = 0 },

            [ProtocolEnum.CTD]          = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 7, StartAdr_RW = 0, RegLen_RW = 0 },
            [ProtocolEnum.Thruster]     = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 4, StartAdr_RW = 0, RegLen_RW = 2 },
            [ProtocolEnum.ISA500]       = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 5, StartAdr_RW = 0, RegLen_RW = 0 },
            [ProtocolEnum.LEC210]       = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 3, StartAdr_RW = 0, RegLen_RW = 2 },
            [ProtocolEnum.Ship2_GPS]    = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 2, StartAdr_RW = 0, RegLen_RW = 0 },

            [ProtocolEnum.Ship2_GPS]    = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 15, StartAdr_RW = 0, RegLen_RW = 0 },
            [ProtocolEnum.Ship2_USBL]   = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 34, StartAdr_RW = 0, RegLen_RW = 0 },
            [ProtocolEnum.NetCameraTCP] = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 0, StartAdr_RW = 0, RegLen_RW = 30 },
            [ProtocolEnum.Insulation]   = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 2, StartAdr_RW = 0, RegLen_RW = 0 },
            [ProtocolEnum.INS]          = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 32, StartAdr_RW = 0, RegLen_RW = 10 },

            [ProtocolEnum.SDI_OSD]      = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 0, StartAdr_RW = 0, RegLen_RW = 30 },
            [ProtocolEnum.HDCS200]      = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 1, StartAdr_RW = 0, RegLen_RW = 2 },
            [ProtocolEnum.PTZCam]       = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 0, StartAdr_RW = 0, RegLen_RW = 20 },
            [ProtocolEnum.Compass3D]    = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 3, StartAdr_RW = 0, RegLen_RW = 2 },
            [ProtocolEnum.Ship2_GPVTG]  = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 4, StartAdr_RW = 0, RegLen_RW = 0 },

            [ProtocolEnum.DTGPS]        = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 15, StartAdr_RW = 0, RegLen_RW = 0 },
            [ProtocolEnum.ModbusReadOnly] = new ProtocolParams { StartAdr_RO = -1, RegLen_RO = -1, StartAdr_RW = 0, RegLen_RW = 0 },
            [ProtocolEnum.DVL]          = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 30, StartAdr_RW = 0, RegLen_RW = 0 },
            [ProtocolEnum.Winch]        = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 5, StartAdr_RW = 0, RegLen_RW = 0 },
            [ProtocolEnum.CMI_Gyro]     = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 2, StartAdr_RW = 0, RegLen_RW = 0 },

            [ProtocolEnum.CMI_USBL]     = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 55, StartAdr_RW = 0, RegLen_RW = 0 },
            [ProtocolEnum.CMI_LBL]      = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 55, StartAdr_RW = 0, RegLen_RW = 0 },
            [ProtocolEnum.CAM4K]        = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 0, StartAdr_RW = 0, RegLen_RW = 10 },
            [ProtocolEnum.PLC_S71200]   = new ProtocolParams { StartAdr_RO = -1, RegLen_RO = -1, StartAdr_RW = -1, RegLen_RW = -1 },
            [ProtocolEnum.Seapath_42]   = new ProtocolParams { StartAdr_RO = 0, RegLen_RO = 20, StartAdr_RW = 0, RegLen_RW = 0 },
        };
    }
}
